package com.by.toolkit.model;

import com.by.toolkit.enums.ErrorCodeEnum;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
@Builder
public class Resp<T> {

    private String code;

    private String msg;

    private T data;

    public Resp() {
    }

    private static final Resp<Void> OK = new Resp<>(ErrorCodeEnum.SUCCESS.getErrorCode());

    public Resp(String code) {
        this.code = code;
    }

    public Resp(ErrorCodeEnum errorCodeEnum) {
        this.code = errorCodeEnum.getErrorCode();
        this.msg = errorCodeEnum.getErrorMessage();
    }


    public Resp(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Resp(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static Resp<Void> ok() {
        return ok(null);
    }

    public static <T> Resp<T> ok(T data) {
        return new Resp<T>(ErrorCodeEnum.SUCCESS.getErrorCode(), StringUtils.EMPTY, data);
    }

    public static <T> Resp<T> fail(String code, String msg, T data) {
        return new Resp<T>(code, msg, data);
    }

    public static Resp<Void> bizFail(String msg) {
        return new Resp<Void>(ErrorCodeEnum.BIZ_FAIL.getErrorCode(), msg);
    }

    public static Resp<Void> paramChkError() {
        return new Resp<Void>(ErrorCodeEnum.PARAM_CHK_ERROR.getErrorCode(), ErrorCodeEnum.PARAM_CHK_ERROR.getErrorMessage());
    }

    public static Resp<Void> sysError() {
        return new Resp<Void>(ErrorCodeEnum.SYS_ERROR.getErrorCode(), ErrorCodeEnum.SYS_ERROR.getErrorMessage());
    }

    public static Resp<Void> httpMethodUnsupport() {
        return new Resp<Void>(ErrorCodeEnum.HTTP_METHOD_UNSUPPORT.getErrorCode(), ErrorCodeEnum.HTTP_METHOD_UNSUPPORT.getErrorMessage());
    }

}
